package com.qifenqian.turbot.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v4.app.DialogFragment
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.support.v4.app.FragmentTransaction
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.qifenqian.turbot.R

/**
 * @author H
 * @createTime 2019/1/2 16:46
 * @description
 * @see
 * @since
 */
abstract class BaseDialogFragment : DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (setDialogStyle() == 0) {
            setStyle(DialogFragment.STYLE_NO_FRAME, R.style.dialog_default_style)
        } else {
            setStyle(DialogFragment.STYLE_NO_FRAME, setDialogStyle())
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // 设置进场动画
        if (setWindowAnimationsStyle() != 0) {
            dialog.window!!.setWindowAnimations(setWindowAnimationsStyle())
        }
        var view: View? = null
        if (view == null) {
            view = inflater.inflate(bindLayoutId(), container, false)
        } else {
            val mViewGroup: ViewGroup = view.parent as ViewGroup
            mViewGroup.removeView(view)
        }
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadData(arguments)
        initView(view)
    }

//    override fun onActivityCreated(savedInstanceState: Bundle?) {
//        super.onActivityCreated(savedInstanceState)
//        loadData(arguments)
//    }

    @LayoutRes
    abstract fun bindLayoutId(): Int

    abstract fun loadData(bundle: Bundle?)

    abstract fun initView(view: View)

    private fun setWindowAnimationsStyle(): Int {
        return 0
    }

    protected fun setDialogStyle(): Int {
        return 0
    }

    protected fun <T : View> createView(context: Context, resId: Int): T {
        return LayoutInflater.from(context).inflate(resId, null) as T
    }

    protected fun setCanceledOnTouchOutside(cancelable: Boolean) {
        if (dialog != null) {
            dialog.setCanceledOnTouchOutside(cancelable)
        }
    }

    protected fun setGravity(gravity: Int) {
        if (dialog != null) {
            dialog.window!!.attributes.gravity = gravity
        }
    }

    /**
     * 设置Dialog窗口width
     */
    protected fun setDialogWidth(width: Int) {
        setDialogWidthAndHeight(width, LinearLayout.LayoutParams.WRAP_CONTENT)
    }

    /**
     * 设置Dialog窗口height
     */
    protected fun setDialogHeight(height: Int) {
        setDialogWidthAndHeight(LinearLayout.LayoutParams.WRAP_CONTENT, height)
    }

    /**
     * 设置Dialog窗口width，height
     */
    protected fun setDialogWidthAndHeight(width: Int, height: Int) {
        if (dialog != null) {
            dialog.window!!.setLayout(width, height)
        }
    }

    fun show(activity: Activity, tag: String) {
        show(activity, null, tag)
    }

    protected fun show(activity: Activity, bundle: Bundle?, tag: String) {
        if (isShowing())
            return
        val transaction: FragmentTransaction = (activity as FragmentActivity).supportFragmentManager.beginTransaction()
        val fragment: Fragment? = activity.supportFragmentManager.findFragmentByTag(tag)
        if (fragment!=null) {
            transaction.remove(fragment)
        }
        if (bundle!=null) {
            arguments = bundle
        }
        show(transaction, tag)
    }

    protected fun isShowing(): Boolean {
        return if (this.dialog != null) {
            this.dialog.isShowing
        } else {
            false
        }
    }
}